﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aspose.Words;
using Aspose.Words.Saving;
using System.Data;

namespace jnlamp.Web
{
    public class OutWord
    {
        public static void outword(string strPath, string outfilename, HttpContext context, DataSet ds)
        {
            Document doc = new Document(strPath);
            doc.MailMerge.ExecuteWithRegions(chds(ds));
            doc.Save(context.Response, outfilename, ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
        }

        private static DataSet chds(DataSet mds)
        {
            DataSet ds = new DataSet();
            DataTable t1 = new DataTable("All");

            DataTable t2 = new DataTable("item");

            t1 = mds.Tables[0].Clone();
            t2 = mds.Tables[0].Copy();
            t1.TableName = "All";
            t2.TableName = "item";
            t2.Columns.Add("level1", typeof(string));
            t2.Columns.Add("level2", typeof(string));
            t2.Columns.Add("level3", typeof(string));
            t2.Columns.Add("level4", typeof(string));
            t2.Columns.Add("isDisasterc", typeof(string));
            t1.Columns.Add("mytime", typeof(string));
            if (mds.Tables[0].Rows.Count > 0)
            {
                t1.Rows.Add(mds.Tables[0].Rows[0].ItemArray);
                if (t1.Rows[0]["investigationTime"].ToString() != "")
                {
                    t1.Rows[0]["mytime"] = DateTime.Parse(t1.Rows[0]["investigationTime"].ToString()).ToLongDateString();
                }
            }
            string value = "";
            for (int i = 0; i < t2.Rows.Count; i++)
            {
                if (bool.Parse(t2.Rows[i]["isDisaster"].ToString()))
                {
                    t2.Rows[i]["isDisasterc"] = "√";
                }
                
                value = t2.Rows[i]["damageLevel"].ToString();
                if (value == "轻度以下")
                {
                    t2.Rows[i]["level1"] = "√";

                }
                else if (value == "轻度")
                {
                    t2.Rows[i]["level2"] = "√";
                }
                else if (value == "中度")
                {
                    t2.Rows[i]["level3"] = "√";
                }
                else if (value == "重度")
                {
                    t2.Rows[i]["level4"] = "√";
                }

            }
            ds.Tables.Add(t1);
            ds.Tables.Add(t2);
            return ds;
        }

        public static void outwordP(string strPath, string outfilename, HttpContext context, DataSet ds,string hzr,string shr,string hzt,string city)
        {
            Document doc = new Document(strPath);
            doc.MailMerge.ExecuteWithRegions(chdsP(ds,hzr,shr,hzt,city));
            doc.Save(context.Response, outfilename, ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
        }

        private static DataSet chdsP(DataSet mds, string hzr, string shr, string hzt, string city)
        {
            DataSet ds = new DataSet();
            DataTable t1 = new DataTable("All");

            DataTable t2 = new DataTable("item");

            //t1 = mds.Tables[0].Clone();
            t2 = mds.Tables[0].Copy();
            t1.TableName = "All";
            t2.TableName = "item";
            //t2.Columns.Add("level1", typeof(string));
            //t2.Columns.Add("level2", typeof(string));
            //t2.Columns.Add("level3", typeof(string));
            t2.Columns.Add("level4", typeof(string));
            //t2.Columns.Add("isDisasterc", typeof(string));
            t1.Columns.Add("hzr", typeof(string));
            t1.Columns.Add("shr", typeof(string));
            t1.Columns.Add("hzt", typeof(string));
            t1.Columns.Add("city", typeof(string));
            DataRow dr = t1.NewRow();
            dr["hzr"] = hzr;
            dr["shr"] = shr;
            if(hzt!="")dr["hzt"]=DateTime.Parse(hzt).ToLongDateString();
             
            dr["city"] = city;
            t1.Rows.Add(dr);
            decimal level1 = 0, level2 = 0, level3=0;
            for (int i = 0; i < t2.Rows.Count; i++)
            {
                level1 = 0; level2 = 0; level3 = 0;
                if (t2.Rows[i]["level1"].ToString() != "") level1 = decimal.Parse(t2.Rows[i]["level1"].ToString());
                if (t2.Rows[i]["level2"].ToString() != "") level2 = decimal.Parse(t2.Rows[i]["level2"].ToString());
                if (t2.Rows[i]["level3"].ToString() != "") level3 = decimal.Parse(t2.Rows[i]["level3"].ToString());
                t2.Rows[i]["level4"] = level1 +level2+level3;
            }
            ds.Tables.Add(t1);
            ds.Tables.Add(t2);
            return ds;
        }
        
        public static void outwordT(string strPath, string outfilename, HttpContext context, DataSet ds)
        {
            Document doc = new Document(strPath);
            doc.MailMerge.ExecuteWithRegions(chdsT(ds));
            doc.Save(context.Response, outfilename, ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
        }
        private static DataSet chdsT(DataSet mds)
        {
            DataSet ds = new DataSet();
            DataTable t1 = new DataTable("All");

            DataTable t2 = new DataTable("item");

            t1 = mds.Tables[0].Clone();
            t2 = mds.Tables[0].Copy();
            t2.Columns.Add("sum", typeof(int));
            t1.TableName = "All";
            t2.TableName = "item";
            t1.Columns.Add("mytime", typeof(string));
            if (mds.Tables[0].Rows.Count > 0)
            {
                t1.Rows.Add(mds.Tables[0].Rows[0].ItemArray);
                if (t1.Rows[0]["investigationTime"].ToString() != "")
                {
                    t1.Rows[0]["mytime"] = DateTime.Parse(t1.Rows[0]["investigationTime"].ToString()).ToLongDateString();
                }
            }
            int f = 0, m = 0;
            for (int i = 0; i < t2.Rows.Count; i++)
            {
                f = 0;
                m = 0;               
                if (t2.Rows[i]["femaleCount"].ToString() != "") f = int.Parse(t2.Rows[i]["femaleCount"].ToString());
                if (t2.Rows[i]["maleCount"].ToString() != "") m = int.Parse(t2.Rows[i]["maleCount"].ToString());
                t2.Rows[i]["sum"] = f + m;
            }
            ds.Tables.Add(t1);
            ds.Tables.Add(t2);
            return ds;
        }
        public static void outwordTc(string strPath, string outfilename, HttpContext context, DataSet ds)
        {
            Document doc = new Document(strPath);
            doc.MailMerge.ExecuteWithRegions(chdsTc(ds));
            doc.Save(context.Response, outfilename, ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
        }

        private static DataSet chdsTc(DataSet mds)
        {
            DataSet ds = new DataSet();
            DataTable t1 = new DataTable("All");

            DataTable t2 = new DataTable("item");

            t1 = mds.Tables[0].Clone();
            t2 = mds.Tables[0].Copy();
            t1.TableName = "All";
            t2.TableName = "item";
            t2.Columns.Add("isstand", typeof(string));
            t1.Columns.Add("mytime", typeof(string));
            if (mds.Tables[0].Rows.Count > 0)
            {
                t1.Rows.Add(mds.Tables[0].Rows[0].ItemArray);
                if (t1.Rows[0]["investigationTime"].ToString() != "")
                {
                    t1.Rows[0]["mytime"] = DateTime.Parse(t1.Rows[0]["investigationTime"].ToString()).ToLongDateString();
                }
            }          
            for (int i = 0; i < t2.Rows.Count; i++)
            {
                if (bool.Parse(t2.Rows[i]["isStandard"].ToString()))
                {
                    t2.Rows[i]["isstand"] = "√";
                }
                
            }
            ds.Tables.Add(t1);
            ds.Tables.Add(t2);
            return ds;
        }
        public static void outwordtf(string strPath, string outfilename, HttpContext context, DataSet ds)
        {
            Document doc = new Document(strPath);
            doc.MailMerge.ExecuteWithRegions(chdstf(ds));
            doc.Save(context.Response, outfilename, ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
        }

        private static DataSet chdstf(DataSet mds)
        {
            DataSet ds = new DataSet();
            DataTable t1 = new DataTable("All");

            DataTable t2 = new DataTable("item");

            t1 = mds.Tables[0].Clone();
            t2 = mds.Tables[0].Copy();
            t1.TableName = "All";
            t2.TableName = "item";
            t2.Columns.Add("level1", typeof(string));
            t2.Columns.Add("level2", typeof(string));
            t2.Columns.Add("level3", typeof(string));
            t2.Columns.Add("level4", typeof(string));
            t1.Columns.Add("mytime", typeof(string));
            if (mds.Tables[0].Rows.Count > 0)
            {
                t1.Rows.Add(mds.Tables[0].Rows[0].ItemArray);
                if (t1.Rows[0]["investigationTime"].ToString() != "")
                {
                    t1.Rows[0]["mytime"] = DateTime.Parse(t1.Rows[0]["investigationTime"].ToString()).ToLongDateString();
                }
            }
            string value = "";
            for (int i = 0; i < t2.Rows.Count; i++)
            {
                
                value = t2.Rows[i]["damageLevel"].ToString();
                if (value == "轻度以下")
                {
                    t2.Rows[i]["level1"] = "√";

                }
                else if (value == "轻度")
                {
                    t2.Rows[i]["level2"] = "√";
                }
                else if (value == "中度")
                {
                    t2.Rows[i]["level3"] = "√";
                }
                else if (value == "重度")
                {
                    t2.Rows[i]["level4"] = "√";
                }

            }
            ds.Tables.Add(t1);
            ds.Tables.Add(t2);
            return ds;
        }

       
    }
}